<?php


namespace app\admin\controller;


use app\common\model\KuaishouApply;
use think\Controller;
use think\facade\Request;
use function GuzzleHttp\Promise\queue;

class Kuaishou extends Controller
{
    protected $middleware = [
        'app\http\middleware\checkAdminToken::class'
    ];

    //授权
    public function cancelAuth($apply_id)
    {

        $apply=KuaishouApply::get($apply_id);

        $user_id=$apply->user_id;
        $user=\app\common\model\User::get($user_id);
        $user->ks_id=null;
        $user->save();

        //删除所有的申请记录
        return     KuaishouApply::where('user_id',$user_id)->delete();

    }


    //改变快手入驻的状态
    public function changeKuaishouStatus()
    {
        $apply_id=Request::param("apply_id");
        $status=Request::param("status");           //1拒绝申请 2 已经同意  -1撤销授权

        if($status==-1){
           $res= $this->cancelAuth($apply_id);
           $this->result($res,200,'已经成功撤销授权');
        }

        $apply=KuaishouApply::get($apply_id);

        if($apply->status!=0){
            $this->result($apply,400,'已经同意或拒绝，请让用户重新申请');
        }else{
            $apply->status=$status;
            $user=\app\common\model\User::get($apply->user_id);
            $user->ks_id=$apply->ks_id;
            $user->save();
            $res=  $apply->save();
            $this->result($res,200,'已同意');
        }
    }

    //列表
    public function index()
    {
        $page  = Request::param('page', 1) ;
        $pageSize = Request::param('pageSize', 20);           //哪一天的数据
        $key= Request::param('key', '') ;

        if ($key){
            $list = KuaishouApply:: where('ks_id|mobile|wechat','like',$key)->page($page, $pageSize)->select();

            $count = KuaishouApply:: where('ks_id|mobile|wechat','like',$key)->count('id');
        }else{
            $list = KuaishouApply::  page($page, $pageSize)->select();

            $count = KuaishouApply::  count('id');
        }

        $data = [
            'list' => $list,
            'page_num' => ceil($count / $pageSize),
            'page' => $page
        ];

        $this->result($data, 200);
        
    }
    
}